package weather_service.weatherprovider;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * WeatherError is an exception class that reports the following:<p>
 * <ol>
 * <li>parsing problems with the xml data returned from weather.com</li>
 * <li>error conditions reported by weather.com</li>
 * </ol>
 *
 * @author Nazmul Idris
 * @since Jan 1, 2007, 5:48:16 PM
 */
public class WeatherError extends Exception {
  static final long serialVersionUID = 3546514703168141265L;

  //:::::::::::::::::::::::::::::::::::::::::::::::
  // constants
  //:::::::::::::::::::::::::::::::::::::::::::::::
  public static enum ErrorTypes{
    NO_ERROR_TYPE_SET,                //this class with no error type set
    UNKNOWN_ERROR,                    //set by caller - unknown exception reported by Weather.com
    NO_LOCATION_PROVIDED_ERROR,       //set by caller - no location provided in query to Weather.com
    INVALID_LOCATION_PROVIDED_ERROR,  //set by caller - invalid location provided by caller in query to Weather.com
    INVALID_PARTNERID_ERROR,          //set by caller - invalid partner id provided by caller in query to Weather.com
    INVALID_PRODUCT_CODE,             //set by caller - invalid product code provided by caller in query to Weather.com
    INVALID_LICENSE_KEY,              //set by caller - invalid license code provided by caller in query to Weather.com
    WEATHER_PARSING_ERROR,            //set by caller - the parser threw a parsing exception when reading the response from a "local" query
    INVALID_WEATHER_REPORT_VERSION,   //set by caller - the response XML returned by weather.com "local" is not the right version that the Weather_20_XMLParser can handle
    SEARCH_LOCATIONS_PARSING_ERROR,   //set by caller - the parser threw a parsing exception when reading the response from a "search" query
    INVALID_SEARCH_VERSION            //set by caller - the response XML returned by weather.com "search" is not the right versoin that the Weather_20_XMLParser can handle
  }

  //:::::::::::::::::::::::::::::::::::::::::::::::
  // data member
  //:::::::::::::::::::::::::::::::::::::::::::::::
  private ErrorTypes _type = ErrorTypes.NO_ERROR_TYPE_SET;  //no type is set by default...

  //:::::::::::::::::::::::::::::::::::::::::::::::
  // constructors...
  //:::::::::::::::::::::::::::::::::::::::::::::::
  public WeatherError(ErrorTypes type, String msg) {
    super(msg);
    _type = type;
  }

  public WeatherError(ErrorTypes type, Throwable t){
    super(t);
    _type = type;
  }

  public WeatherError(ErrorTypes type, Throwable t, String msg){
    super(msg, t);
    _type = type;
  }

  public ErrorTypes getType() {
    return _type;
  }

  public WeatherError(String errorType, String errorValue) {
    super(errorValue);
    if (errorType.equals(Weather_20_XMLParser.NO_ERROR_TYPE_SET))                _type = ErrorTypes.NO_ERROR_TYPE_SET;
    if (errorType.equals(Weather_20_XMLParser.UNKNOWN_ERROR))                    _type = ErrorTypes.UNKNOWN_ERROR;
    if (errorType.equals(Weather_20_XMLParser.NO_LOCATION_PROVIDED_ERROR))       _type = ErrorTypes.NO_LOCATION_PROVIDED_ERROR;
    if (errorType.equals(Weather_20_XMLParser.INVALID_LOCATION_PROVIDED_ERROR))  _type = ErrorTypes.INVALID_LOCATION_PROVIDED_ERROR;
    if (errorType.equals(Weather_20_XMLParser.INVALID_PARTNERID_ERROR))          _type = ErrorTypes.INVALID_PARTNERID_ERROR;
    if (errorType.equals(Weather_20_XMLParser.INVALID_PRODUCT_CODE))             _type = ErrorTypes.INVALID_PRODUCT_CODE;
    if (errorType.equals(Weather_20_XMLParser.INVALID_LICENSE_KEY))              _type = ErrorTypes.INVALID_LICENSE_KEY;
  }

  public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
  }

}//end WeatherError
